﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Web;
using System.Web.Services;
using JOIN.Rights.BLL;
using JOIN.Rights.Model;
using Kevin.Framework;
using Kevin.Framework.Data;
using System.Data;
using System.Diagnostics;
using log4net;

namespace JOIN.Rights.Web.Services
{
    /// <summary>
    /// Summary description for LoginService
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    [System.Web.Script.Services.ScriptService]
    public class AuthService : System.Web.Services.WebService
    {
        private static readonly ILog logger = LogManager.GetLogger(typeof(AuthService));
        System.Diagnostics.Stopwatch watch1 = new Stopwatch();
        /// <summary>
        /// 登录校验
        /// </summary>
        /// <param name="userNO"></param>
        /// <param name="pwd"></param>
        /// <param name="projectID"></param>
        /// <returns></returns>
        [WebMethod]
        public ServiceResult<OnlineUserInfo> Authenticate(string userNO, string pwd, string projectID)
        {
            
            UserBLL bll = new UserBLL(new UserInfo() { UserNO = userNO, UserPwd = pwd.ToMD5String() });
            return bll.Check(projectID);
        }
        

        public ServiceResult<UserInfo> GetUserInfoByTokenID(string tokenID)
        {

            return null;
        }
        /// <summary>
        /// 获取菜单
        /// </summary>
        /// <param name="tokenID"></param>
        /// <returns></returns>
        [WebMethod]
        public List<MenusInfo> GetMenus(string tokenID)
        {
            watch1.Start();
            OnlineUserInfo onlineUser = new OnlineUserInfo() { TokenID = tokenID };

            List<OnlineUserInfo> list = onlineUser.SelectedByProperties<OnlineUserInfo>();
            List<MenusInfo> result = new List<MenusInfo>();
            if (list.Count > 0)
            {
                string projectID = list[0].ProjectID;
                List<FunctionInfo> listFunc = new FunctionInfo() { }.SelectedByProperties<FunctionInfo>();
                List<View_UserFunctionsInfo> userFuncList = new View_UserFunctionsInfo() { ProjectID = projectID, UserNO = list[0].UserNO }.SelectedByProperties<View_UserFunctionsInfo>();

                List<string> funcList = new List<string>();
                foreach (View_UserFunctionsInfo item in userFuncList)
                {
                    funcList.Add(item.FunctionID);
                }

                result = new MenusBLL(null).Load(projectID, "", funcList);
            }
            watch1.Stop();
            logger.Info(string.Format("getmenus:", watch1.Elapsed));
            return result;
        }
        /// <summary>
        /// 修改密码
        /// </summary>
        /// <param name="userNO"></param>
        /// <param name="oldPwd"></param>
        /// <param name="newPwd"></param>
        /// <returns></returns>
        [WebMethod]
        public ServiceResult<UserInfo> ChangePassword(string userNO, string oldPwd, string newPwd)
        {
            return new UserBLL(null).ChangePassword(userNO, oldPwd, newPwd);
        }


    }
}
